#include "messager.h"
#include "ams.h"
#include "log.h"
#include "lvgl.h"
#include "rtc_misc.h"
#include "track_service.h"

LV_IMG_DECLARE(icon_gps);

ACTIVITY_OPS_IMPLEMENT_DECLARE(track, &icon_gps, ACTIVITY_ATTR_FLAG_APP);

// ##
void track_update_name_text(void *content, void *obj, void *data);
void track_update_dots_text(void *content, void *obj, void *data);
void track_update_compress_text(void *content, void *obj, void *data);
void track_update_threshold_text(void *content, void *obj, void *data);
void *track_create_slider(void *content, uint32_t value, void *(*load)(void *a, void *b), void *(*unload)(void *a, void *b));;
void *track_create_dots_text(void *content, void *param);
void *track_create_compress_text(void *content, void *param);
void *track_create_track_line(void *content, void *points, uint32_t cnt);
void *track_create_track_name(void *content, void *param);
void *track_create_threshold_text(void *content, void *param);
void *track_content_unload(void *content, void *data);
uint32_t track_load(lv_point_t **lv_points, track_payload_t **track, uint32_t threshold);
void *track_content_load(void *content, void *data);

static lv_obj_t *name_obj = NULL;
static lv_obj_t *compress_obj = NULL;
static lv_obj_t *dots_obj = NULL;
static lv_obj_t *threshold_obj = NULL;
static track_payload_t *payload = NULL;
static lv_point_t *lv_points = NULL;
static lv_obj_t *lv_lines = NULL;

// ##
static void app_start(void *content)
{

}

// ##
static void app_stop(void *content)
{

}

// ##
static void app_prepare(void *content)
{
    ams_activity_t *activity = (ams_activity_t *)content;
    AMS_UNUSE(activity);
}

/**
 * @description:
 * @param {void} *content
 * @param {void} *data
 * @return {*}
 */
void *track_content_load(void *content, void *data)
{
    track_payload_t *track = NULL;
    uint32_t value = *((uint32_t *)data);
    lv_obj_t *lv_screen = (lv_obj_t *)content;
    uint32_t cnt = track_load(&lv_points, &track, value);

    // create track line
    lv_lines = track_create_track_line(lv_screen, lv_points, cnt);

    track_update_name_text(lv_screen, name_obj, track->name);
    track_update_dots_text(lv_screen, dots_obj, &track->cnt);
    track_update_compress_text(lv_screen, compress_obj, &track->compress);
    track_update_threshold_text(lv_screen, threshold_obj, &track->threshold);

    return track;
}

void *track_content_unload(void *content, void *data)
{
    gpx_free(payload);
    gpx_free(lv_points);
    lv_obj_del(lv_lines);

    return NULL;
}

// ##
static void *app_create(void *content)
{
    lv_obj_t *lv_screen = lv_obj_create(NULL, NULL);

    // create slider for compress-rate adjustment
    uint32_t value = 50;
    track_create_slider(lv_screen, value, track_content_load, track_content_unload);

    // track name string
    name_obj = track_create_track_name(lv_screen, payload);

    // compress rate string
    compress_obj = track_create_compress_text(lv_screen, payload);

    // dots string
    dots_obj = track_create_dots_text(lv_screen, payload);

    // threshold string
    threshold_obj = track_create_threshold_text(lv_screen, payload);

    payload = track_content_load(lv_screen, &value);

    return lv_screen;
}

// ##
static void *app_destroy(void *content)
{
    return NULL;
}

// ##
static void app_event_handle(void *event)
{

}

// ##
static void app_gesture_handle(void *event)
{
    ams_gesture_t evt = *((ams_gesture_t *)event);
    switch(evt)
    {
        case GESTURE_SLIDE_TO_TOP:
            break;
        case GESTURE_SLIDE_TO_BOTTOM:
            break;
        case GESTURE_SLIDE_TO_LEFT:
            break;
        case GESTURE_SLIDE_TO_RIGHT:
            ams_switch_by_name("applist", DIR_SLIDE_TO_RIGHT);
            break;
    }
}
